<?php

namespace App\Library\Service;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopFeedbackLogic;
use App\Library\Requests\Base\SerialNumberRequest;
use App\Library\Requests\Feedback\FeedbackAddRequest;
use App\Library\Requests\Feedback\FeedbackPageRequest;
use Illuminate\Http\JsonResponse;

class FeedbackService extends BaseService
{
    /**
     * @param ShopFeedbackLogic $logic
     */
    public function __construct(ShopFeedbackLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * paging
     * @param FeedbackPageRequest $request
     * @return JsonResponse
     */
    public function paging(FeedbackPageRequest $request): JsonResponse
    {
        return success($this->logic->paging($request));
    }

    /**
     * detail
     * @param SerialNumberRequest $request
     * @return JsonResponse
     */
    public function detail(SerialNumberRequest $request): JsonResponse
    {
        $feedback = $this->logic->findBySerialNumber($request);
        if (is_null($feedback) || $feedback->user_id !== USERID) return fail(433);
        $arr = [];
        foreach ($feedback->images as $v) $arr[] = $v->img_url;
        $feedback->imageArr = $arr;
        unset($feedback->images);
        return success($feedback);
    }

    /**
     * add
     * @param FeedbackAddRequest $request
     * @return JsonResponse
     */
    public function add(FeedbackAddRequest $request): JsonResponse
    {
        $this->logic->add($request);
        OperateLogLogic::add();
        return success();
    }
}
